package com.example.fourtestapp;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.slidingpanelayout.widget.SlidingPaneLayout;

import com.example.fourtestapp.ui.BillFragment;
import com.example.fourtestapp.ui.BusFragment;
import com.example.fourtestapp.ui.LoginActivity;
import com.example.fourtestapp.ui.MilieuFragment;
import com.example.fourtestapp.ui.MyAccFragment;
import com.example.fourtestapp.ui.TrafficLightFragment;
import com.example.fourtestapp.ui.ViolationFragment;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private FragmentManager fm;
    private FrameLayout frameLayout;
    private LinearLayout menuAccount;
    private LinearLayout menuLight;
    private LinearLayout menuBill;
    private LinearLayout menuViolation;
    private LinearLayout menuMilieu;
    private LinearLayout menuBus;
    private LinearLayout menuCenter;
    private LinearLayout menu_quit, quit;
    private ImageView menuShow;
    private SlidingPaneLayout slidingLayout;
    private TextView tcdl, tcyy;

    private BusFragment busFragment;
    private MilieuFragment milieuFragment;
    private ViolationFragment violationFragment;

    private MyAccFragment myAccFragment;
    private BillFragment billFragment;
    private TrafficLightFragment trafficLightFragment;
    private TranslateAnimation ctrlAnimation;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        fm = getSupportFragmentManager();
        ctrlAnimation = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0, TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 2, TranslateAnimation.RELATIVE_TO_SELF, 0);
        ctrlAnimation.setDuration(1000);
        initView();
        initListener();
    }

    private void initView() {
        frameLayout = findViewById(R.id.frameLayout);
        menuAccount = findViewById(R.id.menu_account);
        menuLight = findViewById(R.id.menu_light);
        menuBill = findViewById(R.id.menu_bill);
        menuViolation = findViewById(R.id.menu_violation);
        menuMilieu = findViewById(R.id.menu_milieu);
        menuBus = findViewById(R.id.menu_bus);
        menuCenter = findViewById(R.id.menu_center);
        menuShow = findViewById(R.id.menu_show);
        slidingLayout = findViewById(R.id.sliding_layout);
        menu_quit = findViewById(R.id.menu_quit);
        tcyy = findViewById(R.id.tcyy);
        tcdl = findViewById(R.id.tcdl);
        quit = findViewById(R.id.quit);
        sharedPreferences = MainActivity.this.getSharedPreferences("user", 0);
        editor = sharedPreferences.edit();
    }

    private void initListener() {
        menuAccount.setOnClickListener(this);
        menuLight.setOnClickListener(this);
        menuBill.setOnClickListener(this);
        menuViolation.setOnClickListener(this);
        menuMilieu.setOnClickListener(this);
        menuBus.setOnClickListener(this);
        menuCenter.setOnClickListener(this);
        menuShow.setOnClickListener(this);
        menu_quit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                quit.setVisibility(View.VISIBLE);
                quit.startAnimation(ctrlAnimation);
                quit.setEnabled(false);
            }
        });

        tcyy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tcdl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, LoginActivity.class));
                editor.putString("autologin", "");
                editor.commit();
                finish();
                overridePendingTransition(R.anim.anim_test_in, R.anim.anim_test_out);
            }
        });

        slidingLayout.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
            @Override
            public void onPanelSlide(@NonNull View panel, float slideOffset) {
            }

            @Override
            public void onPanelOpened(@NonNull View panel) {
                quit.setVisibility(View.GONE);
                quit.setEnabled(true);
            }

            @Override
            public void onPanelClosed(@NonNull View panel) {
                quit.setVisibility(View.GONE);
            }
        });
    }


    public void MenuController() {
        if (slidingLayout.isOpen()) {
            slidingLayout.closePane();
        } else {
            slidingLayout.openPane();
        }
    }

    private void Hide(FragmentTransaction transaction) {
        if (busFragment != null) {
            transaction.hide(busFragment);
        }
        if (milieuFragment != null) {
            transaction.hide(milieuFragment);
        }
        if (violationFragment != null) {
            transaction.hide(violationFragment);
        }
        if (myAccFragment != null) {
            transaction.hide(myAccFragment);
        }
        if (billFragment != null) {
            transaction.hide(billFragment);
        }
        if (trafficLightFragment != null) {
            transaction.hide(trafficLightFragment);
        }

    }

    @Override
    public void onClick(View view) {
        FragmentTransaction transaction = fm.beginTransaction();
        switch (view.getId()) {
            case R.id.menu_show:
                MenuController();
                break;
            case R.id.menu_account:
                if (myAccFragment == null) {
                    myAccFragment = new MyAccFragment();
                    transaction = fm.beginTransaction().add(R.id.frameLayout, myAccFragment);
                }
                Hide(transaction);
                transaction.show(myAccFragment).commit();
                break;
            case R.id.menu_light:
                if (trafficLightFragment == null) {
                    trafficLightFragment = new TrafficLightFragment();
                    transaction = fm.beginTransaction().add(R.id.frameLayout, trafficLightFragment);
                }
                Hide(transaction);
                transaction.show(trafficLightFragment).commit();
                break;
            case R.id.menu_bill:
                if (billFragment == null) {
                    billFragment = new BillFragment();
                    transaction = fm.beginTransaction().add(R.id.frameLayout, billFragment);
                }
                Hide(transaction);
                transaction.show(billFragment).commit();
                break;
            case R.id.menu_violation:
                if (violationFragment == null) {
                    violationFragment = new ViolationFragment();
                    transaction = fm.beginTransaction().add(R.id.frameLayout, violationFragment);
                }
                Hide(transaction);
                transaction.show(violationFragment).commit();
                break;
            case R.id.menu_milieu:
                if (milieuFragment == null) {
                    milieuFragment = new MilieuFragment();
                    transaction = fm.beginTransaction().add(R.id.frameLayout, milieuFragment);
                }
                Hide(transaction);
                transaction.show(milieuFragment).commit();
                break;
            case R.id.menu_bus:
                if (busFragment == null) {
                    busFragment = new BusFragment();
                    transaction = fm.beginTransaction().add(R.id.frameLayout, busFragment);
                }
                Hide(transaction);
                transaction.show(busFragment).commit();
                break;
            case R.id.menu_center:
                break;
        }
        MenuController();
    }
}
